<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let school = {
            name: "山东南翔",
            students: [
                {
                    name: '国强子',
                    age: 25,
                    fav: ['唱歌', '游戏']
                },
                {
                    name: '铁锤',
                    age: 28,
                    fav: ['rap', '游戏']
                }
            ],
            teachers: [
                {
                    name: '唐老师',
                    age: 45,
                    fav: ['挖掘机', '开车']
                },
                {
                    name: 'tony老师',
                    age: 28,
                    fav: ['洗', '剪', '吹']
                }
            ]
        }
        //1.分别完成上面school对象的深拷贝和浅拷贝。可尝试使用函数递归的方式
        /*----------------浅拷贝---------------*/
        function shadowCopy(obj) {
            let copy = Object.assign({}, obj)
            return copy
        }
        let school1 = school
        let school2 = shadowCopy(school)

        function shadowCopy1(obj) {
            let copy = {}
            for (key in obj) {
                copy[key] = obj[key]
            }
            return copy
        }
        let school3 = shadowCopy1(school)
        /*----------------深拷贝---------------*/
        function deepCopy(obj) {
            let objStr = JSON.stringify(obj)
            return JSON.parse(objStr)
        }
        let school4 = deepCopy(school)

        function dp(obj, copy) {
            //key = fav
            for (key in obj) {
                if (typeof obj[key] == "object") {
                    copy[key] =  Array.isArray(obj[key]) ? [] : {}
                    // if(Array.isArray(obj[key])){
                    //     copy[key] = []
                    // }else{
                    //     copy[key] = {}
                    // }
                    dp(obj[key], copy[key])
                } else {
                    copy[key] = obj[key]
                }
            }
        }

       
        let s1 = {
            name: '唐老师',
            age: 45,
            fav: {a:1,b:2},
            skill:[1,2,3,4]
        }
        let s2 = {};
        dp(s1,s2)
        
        //2.给定三个对象模板，创建模板对应的构造函数，通过继承实现。
        function AttackBase(hp,mp,gjl){
            this.hp = hp
            this.mp = mp
            this.gjl = gjl
            this.attack = function(){
                console.log('攻击了对方')
            }
        }
        //对位传参数的方式要把自己定义的参数放在最后面
        function Hero(hp,mp,gjl,name){
            AttackBase.apply(this,arguments)
            this.type = 'hero'
            this.name = name
        }

        function Soldier(hp,mp,gjl,type){
            AttackBase.apply(this,arguments)
            this.type = type
            
        }
        let ys = new Hero(500,0,100,'亚索')
        let jcb = new Soldier(100,10,10,'近程兵')
    </script>
</body>

</html>